package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.Reservation_Meal;

public class Reservation_MealDAO extends ModelDAO {

	private Connection connection;
	
	public Reservation_MealDAO (){
		super();
		connection = getConnection();
	}
	
	public void closeConnection(){
		super.closeConnection();
	}
	
	public Reservation_Meal select(String meal_name, int reservation_id) {
		Reservation_Meal rm = null;
		try{
			String consulta = "SELECT * FROM RESERVATIONS_MEALS WHERE MEAL_NAME=?"+
							" AND RESERVATION_ID=?";
			PreparedStatement statement = this.connection.prepareStatement(consulta);
			
			try{statement.setString(1, meal_name);}
			catch(Exception e){e.printStackTrace();}
			
			try{statement.setInt(1, reservation_id);}
			catch(Exception e){e.printStackTrace();}
			
			ResultSet result = statement.executeQuery();
			rm = new Reservation_Meal();
			while(result.next()){
				rm.setReservation_id(result.getInt(1));
				rm.setMeal_name(result.getString(2));
				rm.setQuantity(result.getInt(3));
			}
		    result.close();
		    statement.close();
		}
		catch(SQLException e){e.printStackTrace();}
		return rm;
	}
	
	public void create(Reservation_Meal rm){
		if(select(rm.getMeal_name(),rm.getReservation_id())!=null)return;
		
		try{
			String consulta = "INSERT INTO RESERVATIONS_MEALS "
				+"(MEAL_NAME, RESERVATION_ID, QUANTITY)"
				+ "VALUES " + "(?,?,?)";
			PreparedStatement statement = this.connection.prepareStatement(consulta);
			
			try{statement.setString(1, rm.getMeal_name());}
			catch(Exception e){e.printStackTrace();}
			
			try{statement.setInt(2, rm.getReservation_id());}
			catch(Exception e){e.printStackTrace();}
			
			try{statement.setInt(3, rm.getQuantity());}
			catch(Exception e){e.printStackTrace();}
			
		    statement.executeUpdate();
		    statement.close();
		}
		catch(SQLException e){e.printStackTrace();}	
	}

	public void update(Reservation_Meal rm){
		if(select(rm.getMeal_name(),rm.getReservation_id())!=null)return;
		
		try{
			String consulta = "UPDATE RESERVATIONS_MEALS "+
				"SET QUANTITY = ?"+
				" WHERE MEAL_NAME = ? AND RESERVATION_ID = ?";
			PreparedStatement statement = this.connection.prepareStatement(consulta);

			try{statement.setInt(1, rm.getQuantity());}
			catch(Exception e){e.printStackTrace();}		
			
			try{statement.setString(2, rm.getMeal_name());}
			catch(Exception e){e.printStackTrace();}
			
			try{statement.setInt(3, rm.getReservation_id());}
			catch(Exception e){e.printStackTrace();}
			
			statement.executeUpdate();
		    statement.close();
		}
		catch(SQLException e){e.printStackTrace();}
	}
	
	public void delete(String meal_name,int reservation_id){
		if(select(meal_name,reservation_id)==null)return;
		
		try{
			String consulta = "DELETE FROM RESERVATIONS_MEALS WHERE MEAL_NAME=?"+
								" AND RESERVATION_ID=?";
			PreparedStatement statement = this.connection.prepareStatement(consulta);
			
			try{statement.setString(1, meal_name);}
			catch(Exception e){e.printStackTrace();}
			
			try{statement.setInt(2,reservation_id);}
			catch(Exception e){e.printStackTrace();}
			
			statement.executeUpdate(consulta);
			statement.close();
		}
		catch(SQLException e){e.printStackTrace();}
	}
}
